import { getPermissionAPI } from '@/api/login'
import router, { DynamicRoutes } from '@/router/index.js'
import dynamicRouter from '@/router/dynamic-router'
import { recursionRouter, setDefaultRoute } from '@/utils/recursion-route'
export default {
    namespaced: true,
    state: {
        token: sessionStorage.getItem('token') || '',
        permissionList: [],
        sidebarMenu: [],
        currentMenu: sessionStorage.getItem('currentMenu') || "",
        userName: sessionStorage.getItem('userName') || ''
    },
    getters: {},
    mutations: {
        LOGIN(state, token) {
            console.log(token);
            state.token = token
            sessionStorage.setItem('token', token)

        },
        SET_PERMISSION(state, routes) {

            state.permissionList = routes

        },
        CLEAT_PERMISSiON(state) {
            state.permissionList = []
        },
        SET_MENU(state, menu) {
            state.sidebarMenu = menu
        },
        CLEAR_MENU(state) {
            state.sidebarMenu = []
        },
        CLEAT_TOKEN(state) {
            state.token = ''
        },
        SET_CURRENTMENU(state, data) {
            state.currentMenu = data
            sessionStorage.setItem('currentMenu', data)
        }
    },
    actions: {
        async FETCH_PERMISSION({ commit, state },) {

            // 用户登录，根据角色返回菜单
            let premissionList = await getPermissionAPI()
     

            // let premissionList = {
            //     data: [
            //         {
            //             name: "实验组织",
            //             children: [
            //                 {
            //                     name: "实验管理"
            //                 },
            //                 {
            //                     name: "实验组织"
            //                 },
            //                 {
            //                     name: "席位登录"
            //                 },
            //             ]
            //         },
            //         {
            //             name: "实验场景",
            //             children: [
            //                 {
            //                     name: "场景管理"
            //                 },
            //                 {
            //                     name: "场景编辑"
            //                 },

            //             ]
            //         },
            //         {
            //             name: "资源管理",
            //             children: [
            //                 {
            //                     name: "兵力模型"
            //                 },
            //                 {
            //                     name: "算法模型"
            //                 },
            //                 {
            //                     name: "知识图谱"
            //                 },

            //                 {
            //                     name: "GIS管理"
            //                 },
            //                 {
            //                     name: "权限管理"
            //                 },

            //             ]
            //         },
            //         {
            //             name: "实验部署",
            //             children: [
            //                 {
            //                     name: "业务规划"
            //                 },
            //                 {
            //                     name: "网络规划"
            //                 },
            //                 {
            //                     name: "协议配置"
            //                 },

            //                 {
            //                     name: "交互仿真"
            //                 },
            //                 {
            //                     name: "交互资源"
            //                 },

            //             ]
            //         },
            //         {
            //             name: "显控配置",
            //             children: [
            //                 {
            //                     name: "布局设置"
            //                 },
            //                 {
            //                     name: "页面展示"
            //                 },
            //                 {
            //                     name: "协议配置"
            //                 },

            //                 {
            //                     name: "交互仿真"
            //                 },
            //                 {
            //                     name: "交互资源"
            //                 },

            //             ]
            //         },
            //     ]
            // }


            let children = []
            let MainContainer = { children: [] }
            let routes = []
            routes = recursionRouter(premissionList.data, dynamicRouter)
            // console.log(premissionList.data);
            // console.log(dynamicRouter);
            // console.log(routes, 8888);
            MainContainer = DynamicRoutes.find(v => v.name == '/')

            MainContainer.children = []
            children = MainContainer.children
            children.push(...routes)
            // 生成菜单

            commit('SET_MENU', children)
            // 设置默认路由
            setDefaultRoute([MainContainer])
            // 初始化路由
            let initRoutes = router.options.routes
            console.log(DynamicRoutes);
            DynamicRoutes.forEach(v => {
                router.addRoute(v)
            })


            commit('SET_PERMISSION', [...initRoutes, ...DynamicRoutes])
        },

        // 退出
        logout({ commit, state }) {

            commit('CLEAR_MENU')
            commit('CLEAT_PERMISSiON')
            commit('CLEAT_TOKEN')
            let BASE_URL = sessionStorage.getItem('BASE_URL')
            sessionStorage.clear()
            sessionStorage.setItem('BASE_URL', BASE_URL)

            // router.replace('/login')
            router.push('/login')

        }
    }
}